
import { app } from "../../../base";
import { AnalyticsData } from "./IAnalyticsAble";

export type ADConfig = {
    bannerUnitIds:string[],
    interstitialUnitIds:string[],
    rewardVideoUnitIds:string[]
}

export enum ADType {
    BANNER,
    INTERSTITIAL,
    REWARD_VIDEO,
    NATIVE
}

export enum ADEvent {
    LOAD,
    LOAD_COMPLETE,
    LOAD_FAIL,
    SHOW,
    SHOW_COMPLETE,
    SHOW_FAIL,
    CLOSE_BY_USER,
}

export type AdFrom = {
    /**
     * 广告调起的视图
     */
    view?:string, 
    /**
     * 广告调起的动作（按钮）
     */
    action?:string
}&AnalyticsData

/**
 * 广告能力
 */
export class IADAble{
    protected _type:string;
    protected _config:ADConfig;
    /**
     * 初始化
     * @param type 功能类型，如：wx;tt等
     */
    init(type:string, config:ADConfig):IADAble{
        if(!this._type){
            this._type = type;
            this._config = config;
        }
        return this;
    }

    /**
     * 上报广告事件
     * @param type 广告类型
     * @param event 广告事件
     * @param from 广告来源
     * @param reason 广告失败原因
     */
    protected _report(type:ADType, event:ADEvent, from:AdFrom, reason?:string):void {
        from = from || {};
        from._type = type;
        from._event = event;
        if(reason) {
            from._reason = reason;
        }
        app.publisher.analytics.report("ad", from);
    }
    /**
     * 加载Banner
     */
    loadBanner(from:AdFrom, caller?: any, onSuccess?: Function, onFail?: Function):void{
        console.log("加载banner");
        onSuccess && onSuccess.apply(caller);
    }
    /**
     * 显示banner
     */
    showBanner(from:AdFrom, caller?: any, onSuccess?: Function, onFail?: Function, ...args:any[]):void{
        console.log("显示banner");
        onSuccess && onSuccess.apply(caller);
    }
    /**
     * 隐藏banner
     */
    hideBanner():void{
        console.log("隐藏banner");
    }
    /**
     * 显示激励视频 
     * @param caller 可选参数，回调this
     * @param onSuccess 可选参数，成功回调
     * @param onFail 可选参数，失败回调
     * @param unitIdIndex 可选参数，广告索引
     * @param from 可选参数，来源标识，会上报到统计分析
     */
    showRewardVideo(from:AdFrom, caller?: any, onSuccess?: Function, onFail?: Function, ...args:any[]):void{
        this._report(ADType.REWARD_VIDEO, ADEvent.SHOW, from);
        this._report(ADType.REWARD_VIDEO, ADEvent.SHOW_COMPLETE, from);
        onSuccess && onSuccess.apply(caller);
    }
    
    /**
     * 加载插屏广告
     * @param unitId 
     */
    loadInterstitial(from:AdFrom, caller?: any, onSuccess?: Function, onFail?: Function, ...args:any[]):void{
        this._report(ADType.INTERSTITIAL, ADEvent.SHOW, from);
        this._report(ADType.INTERSTITIAL, ADEvent.SHOW_COMPLETE, from);
        onSuccess && onSuccess.apply(caller);
    }

    /**
     * 显示插屏广告
     * @param unitId 
     */
    showInterstitial(from:AdFrom, caller?: any, onSuccess?: Function, onFail?: Function, ...args:any[]):void{
        console.log("显示插屏广告");
        onSuccess && onSuccess.apply(caller);
    }
}
